package com.person.product.service.impl;

import java.util.List;

import com.person.commons.utils.DAO;
import com.person.commons.utils.FlipPageInfo;
import com.person.link.model.Link;
import com.person.product.model.Product;
import com.person.product.service.ProductService;
import com.person.title.model.TitleFunc;


public class ProductServiceImpl implements ProductService{
	
	private DAO dao;

	public DAO getDao() {
		return dao;
	}

	public void setDao(DAO dao) {
		this.dao = dao;
	}
	/**
	 * 保存产品对象
	 */
	public void saveProduct(Product product){
		
		try{
			dao.save(product);
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(e+"保存Product对象出现异常");
		}
	}
	/**
	 * 通过产品对象ID主键查找对象
	 */
	public Product findProductById(String productId){
		
		try{
			return (Product) dao.find(Product.class,productId);
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(e+"查找Product对象出现异常");
		}
		return new Product();
	}
	/**
	 * 查找产品对象分页列表出来
	 */
	public FlipPageInfo findAllProductInfo(FlipPageInfo fpi){
		
		try{
			return dao.findWithPaging("select t.funcName,p.productName,p.productFlag,p.productTop," +
									"p.productClick,p.userDate,b.userName,p.productId from Product p,TitleFunc t,BaseUser b " +
									"where p.funcId = t.funcId and b.userId=p.userId order by p.funcId,p.userDate desc", fpi);
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(e+"列表Product对象出现异常");
		}
		return fpi;
	}
	/**
	 * 通过查询条件查找产品对象，分页列表
	 */
	public FlipPageInfo findProductByInfo(String hql,FlipPageInfo fpi){
		
		try{
			return dao.findWithPaging(hql, fpi);
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(e+"查询Product对象出现异常");
		}
		return fpi;
	}
	/**
	 * 删除对象
	 */
	public void deleteProduct(Product product){
		
		try{
			dao.delete(product);
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(e+"删除Product对象出现异常");
		}
	}
	/**
	 * 修改对象
	 */
	public void updateProduct(Product product){
		
		try{
			dao.update(product);
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(e+"修改Product对象出现异常");
		}
	}
	/**
	 * 根据funcId从栏目功能表TitleFunc查找功能名
	 */
	public TitleFunc findTitleFuncByFuncId(String funcId){
		try{
			return (TitleFunc) dao.find(TitleFunc.class, funcId);
		}catch(Exception e){
			System.out.println("栏目功能表TitleFunc查找功能名出现异常");
		}
		return new TitleFunc();
	}
	
	/**
	 * 根据funcId查找Product
	 */
	public List<Product> findProductByFuncId(String funcId) {
		return this.dao.find("from Product where funcId='"+funcId+"' and productFlag='0' and productTop='1' order by userDate") ;
	}
	
	/**
	 * 查找产品信息，分页列表
	 * @param fpi
	 * @return
	 */
	public FlipPageInfo findProductPageInfoByFuncId (String funcId ,FlipPageInfo fpi) {
		
		return this.dao.findWithPaging("from Product where funcId='"+funcId+"' and productFlag='0' order by productOrder desc", fpi) ;
	}
	
}
	